package com.jrx.anytxn.transaction.controller.cms;


import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.transaction.bean.DealMistakeBean;
import com.jrx.anytxn.transaction.constant.TransErrMsgConstant;
import com.jrx.anytxn.transaction.dto.mistakeLog.MistakeLogRes;
import com.jrx.anytxn.transaction.service.IMistakeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 与页面相关的差错处理API接口
 *
 * @author yf
 * date 2019/10/24 0024
 * @version V1.0
 **/
@Api(tags = "与页面相关的差错处理功能API接口")
@RestController
@RequestMapping(value = "/transaction/dealManager/mistake")
public class MistakeLogController {
    private static final Logger logger = LoggerFactory.getLogger(MistakeLogController.class);

    @Autowired
    private IMistakeService mistakeService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID分页查询差错流水表信息", notes = "根据租户ID分页查询差错流水表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<MistakeLogRes>> getAllByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<MistakeLogRes>> result = new TxnRespResult<>();
        TxnPage<MistakeLogRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = mistakeService.findMistakeLogByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.getFail(TxnRespCode.ERROR.getCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "处理差错记录", notes = "处理差错记录")
    @PutMapping("/")
    public TxnRespResult<Boolean> editSystemParameter(@RequestBody DealMistakeBean dealMistakeBean) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            mistakeService.dealMistakeRecord(dealMistakeBean);
            result.getSuccess(true);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.getFail(TxnRespCode.ERROR.getCode(), TxnRespCode.ERROR.getUserTips());
        }
        return result;
    }


}
